/* QAD_ProductStructureCostSummary.p - SUMMARIZED BILL OF MATERIAL COST REPORT*/
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: QAD_TransactionsAccountingReport.p 1046 2010-09-10 09:08:19Z jvz  $: */
/*                                                                            */
/* Report matches legacy bmpsrp05.p for use by QAD Reporting Framework.       */
/*                                                                            */

{mfsubdirs.i}
{{&US_BBI}mfdeclre.i}
{{&US_BBI}gplabel.i}

{com/qad/shell/report/dsReportRequest.i}
{com/qad/shell/report/ReportConstants.i}

/*============================================================================*/
/* ****************************  TEMP TABLES  ******************************* */
/*============================================================================*/

/* TEMP-TABLE DEFINITION BLOCK */

define temp-table tt_sct_det no-undo like sct_det
   field item_code    as   character
   field parent_bom   as   character
   field altpart      like ptal_mstr.ptal_alt_part
   field l_comp       as   character
   field desc1        like pt_mstr.pt_desc1
   field desc2        like pt_mstr.pt_desc2
   field gl_cost_site like in_mstr.in_gl_cost_site
   field unique_id    as   integer
.

define temp-table tt_pt_mstr no-undo like pt_mstr
   field sum_usage    like ps_mstr.ps_qty_per
   field mtl          like sct_det.sct_mtl_tl
   field lbr          like sct_det.sct_lbr_tl
   field bdn          like sct_det.sct_bdn_tl
   field ovh          like sct_det.sct_ovh_tl
   field sub          like sct_det.sct_sub_tl
   field unittot      like sct_det.sct_cst_tot
   field extmtl       like sct_det.sct_mtl_tl
   field extlbr       like sct_det.sct_lbr_tl
   field extbdn       like sct_det.sct_bdn_tl
   field extovh       like sct_det.sct_ovh_tl
   field extsub       like sct_det.sct_sub_tl
   field exttot       like sct_det.sct_cst_tot
   field gl_cost_site like in_mstr.in_gl_cost_site
   field unique_id    as   integer
   field ref_id       like tt_sct_det.unique_id
.

define temp-table ttReportOptions no-undo
   field eff_date  as   date
   field max_level as   integer
   field newpage   as   mfc_ctrl_mfc_logical
   field site      like si_mstr.si_site
   field csset     like sct_det.sct_sim
.

define dataset dsReportResults for tt_sct_det, tt_pt_mstr, ttReportOptions.

/*============================================================================*/
/* *****************************  MAIN BLOCK  ******************************* */
/*============================================================================*/
define input parameter runReport    as logical no-undo.
define input parameter reportHandle as handle  no-undo.
define input parameter dataset      for dsReportRequest.
define output parameter dataset-handle phReportResults.

{com/qad/shell/report/reporting.i}

define variable vhDS as handle    no-undo.

empty temp-table tt_sct_det      no-error.
empty temp-table tt_pt_mstr      no-error.
empty temp-table ttReportOptions no-error.

for first ttReportRequest no-lock:

   vhDS = dataset dsReportResults:handle.
   run FillMetaData in reportHandle(
      vhDS, "QAD_ProductStructureCostSummary.meta.xml").

   run SetReportMetaData (input reportHandle).

   if runReport then do:
      run RunReport
         (output dataset-handle phReportResults).
   end.

end.

PROCEDURE SetReportMetaData:

   define input parameter  ipReportHandle as handle.

   run SetFieldMetaParameter in ipReportHandle ("ttReportOptions",
         "newpage", "DataFormat", getLabel("YES/NO")).

   run SetFieldMetaParameter in ipReportHandle("ttReportOptions",
         "site", "DefaultValue", global_site).

   run SetFieldMetaParameter in ipReportHandle("ttReportOptions",
         "eff_date", "DefaultValue", TODAY).
   
   for first si_mstr
      where si_mstr.si_domain = global_domain and si_site = global_site
   no-lock:
      run SetFieldMetaParameter in ipReportHandle("ttReportOptions",
         "csset", "DefaultValue", si_cur_set).
   end. /* FOR FIRST si_mstr */

END PROCEDURE. /* PROCEDURE ModifyMetaData */

/* DATA RETRIEVING LOGIC BLOCK */
PROCEDURE RunReport:

   define output parameter dataset-handle phReportResults.

   /* RETRIEVE THE DATA FROM DATABASE */
   define variable queryString   as character no-undo.
   define variable hMainQuery    as handle    no-undo.
   define query    MainQuery     for so_mstr.

   assign
      hMainQuery  = query MainQuery:handle
      l_site      = GetFilterValue("ttReportOptions",
                                  "site", global_site)
      csset       = GetFilterValue("ttReportOptions",
                                  "csset", "")
      .

   for first si_mstr
      where si_mstr.si_domain = global_domain and  si_site = l_site
   no-lock:
   end. /* FOR FIRST si_mstr */

   if not available si_mstr
   then do:

      /* SITE DOES NOT EXIST */
      {us/bbi/pxmsg.i &MSGNUM=708 &ERRORLEVEL=3}
      return.
   end. /* IF NOT AVAILABLE si_mstr */

   if si_db <> global_db
   then do:

      /* SITE IS NOT ASSIGNED TO THIS DOMAIN */
      {us/bbi/pxmsg.i &MSGNUM=6251 &ERRORLEVEL=3}
      return.
   end. /* IF si_db <> global_db */

   if csset = ""
   then do:

      /* BLANK NOT ALLOWED */
      {us/bbi/pxmsg.i &MSGNUM=40 &ERRORLEVEL=3}
      return.
   end. /* IF csset = "" */

   for first cs_mstr
      where cs_mstr.cs_domain = global_domain and  cs_set = csset
   no-lock:
   end.

   if not available cs_mstr
   then do:

      /* COST SET DOES NOT EXIST */
      {us/bbi/pxmsg.i &MSGNUM=5407 &ERRORLEVEL=3}
      return.
   end. /* IF NOT AVAILABLE cs_mstr */

   for first bom_mstr
      where bom_mstr.bom_domain = global_domain
      and bom_parent >= part
      and bom_parent <= part1
   no-lock:
   end. /* FOR FIRST bom_mstr */

   for first pt_mstr
      where pt_mstr.pt_domain = global_domain and  pt_part >=part
      and pt_part <= part1
   no-lock:
   end. /* FOR FIRST pt_mstr */









/* queryString = "for each so_mstr no-lock where so_domain = "
                  + quoter(global_domain).

   run AddAllConditions in reportHandle
      ("so_mstr", input-output queryString).

   queryString = queryString + ":".

   hMainQuery:query-prepare(queryString).
   hMainQuery:query-open().
   hMainQuery:get-next().

   repeat while not hMainQuery:query-off-end:

      hMainQuery:get-next().
   end. /* REPEAT QUERY */

   hMainQuery:query-close().
*/
   delete object hMainQuery.
   hReportResults = dataset dsReportResults:handle.

END PROCEDURE.
